package com.qiche.controller.wx;

import com.qiche.aspect.Result;
import com.qiche.dto.SimpleMemberDto;
import com.qiche.enums.RedisKeyPrefixEnum;
import com.qiche.enums.ResultEnum;
import com.qiche.model.StoreMember;
import com.qiche.model.StoreMemberCar;
import com.qiche.redis.RedisService;
import com.qiche.requestpram.MemberRegisterURL;
import com.qiche.service.StoreMemberCarService;
import com.qiche.service.StoreMemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Linzewen
 * @Date: 2018/5/22 0022 15:48
 */
@RestController
@RequestMapping("wx/home")
public class HomeWxController {

	@Autowired
	private HttpSession session;

	@Autowired
	private StoreMemberCarService storeMemberCarService;

	@Autowired
	private StoreMemberService storeMemberService;

	@Autowired
	private RedisService redisService;

	/**
	 * 我的
	 * 
	 * @return
	 */
	@PostMapping("")
	public Result<?> getMember(@RequestParam String accessToken, @RequestParam String openid, String unionid,
			@RequestParam Long merchantId, StoreMember storeMember) {
		SimpleMemberDto simpleMemberDto = new SimpleMemberDto();
		BeanUtils.copyProperties(storeMember, simpleMemberDto);
		List<StoreMemberCar> memberCars = storeMemberCarService.getCarsByStoreMemberId(storeMember.getId());
		if (memberCars != null && memberCars.size() != 0) {
			StoreMemberCar memberCar = memberCars.get(0);
			BeanUtils.copyProperties(memberCar, simpleMemberDto);
		}
		return Result.createByResultEnum(ResultEnum.SUCCESS, simpleMemberDto);
	}

	/**
	 * 微信用户更换绑定的手机号码
	 * 
	 * @param memberRegisterURL
	 * @return
	 */
	@PutMapping("/modifyPhone")
	public Result<?> updateMemberPhone(@RequestBody MemberRegisterURL memberRegisterURL) {
		StoreMember member = (StoreMember) session.getAttribute("member");
		if (member == null) {
			return Result.createByResultEnum(ResultEnum.MEMBER_LOGIN_PAST);
		}
		String code = memberRegisterURL.getCode();
		String memberPhone = memberRegisterURL.getMemberPhone();
		if (StringUtils.isEmpty(code) || StringUtils.isEmpty(memberPhone)) {
			return Result.createByResultEnum(ResultEnum.PARAM_ERROR);
		}
		StoreMember updatePhoneMember = storeMemberService.selectByPhoneAndMerchantId(memberPhone,
				member.getMerchantId().intValue());
		if (updatePhoneMember != null) {
			return Result.createByResultEnum(ResultEnum.MEMBER_EXIST_BY_PHONE);
		}
		//String redisCode = redisManager.getString(RedisKeyPrefixEnum.SEND_MEMBER_WX_BIND_PHONE + memberPhone);

		String redisCode=redisService.get(RedisKeyPrefixEnum.SEND_MEMBER_WX_BIND_PHONE + memberPhone).toString();
		if (redisCode == null) {
			return Result.createByResultEnum(ResultEnum.MEMBER_CODE_TIME_OUT);
		}
		if (!redisCode.equals(code)) {
			return Result.createByResultEnum(ResultEnum.VERIFY_CODE_ERROR);
		}
		member.setPhone(memberPhone);
		// 可以更新
		int i = storeMemberService.updateNotNull(member);
		if (i > 0) {
			return Result.createByResultEnum(ResultEnum.SUCCESS);
		} else {
			return Result.createByResultEnum(ResultEnum.FAILURE);
		}
	}

}
